<template>
  <div class="roleAdd">
    <el-form :model="personalrole" :rules="rules" label-width="80px">
        <el-form-item label="角色名称" prop="roleName">
            <el-input ref="roleName" v-model="personalrole.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色值" prop="roleVal">
            <el-input ref="roleVal" v-model="personalrole.roleVal"></el-input>
        </el-form-item>
        <el-form-item label="描        述" prop="description">
            <el-input ref="description" v-model="personalrole.description"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="isEnable">
            <el-switch ref="isEnable" v-model="personalrole.isEnable"></el-switch>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" class="save" @click="save(personalrole.roleBh)">保存</el-button>
            <el-button class="quxiao" @click="closeLayer">取消</el-button>
        </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "roleAdd",
  props: ["personalrole"],
  data() {
    return {
      rules: {
        roleName: [
          { required: true, message: "请填写角色名", trigger: "blur" },
          { min: 3, max: 20, message: "长度3-20", trigger: "blur" }
        ],
        roleVal: [
          { required: true, message: "请填选择色值", trigger: "blur" },
          { max: 20, message: "不能超出长度20", trigger: "blur" }
        ]
      }
    };
  },
  mounted: function() {
    this.$nextTick(() => {});
  },
  methods: {
    //关闭layer弹窗
    closeLayer() {
      layer.closeAll();
    },
    //新增
    save(roleBh) {
      // if (this.$refs.roleName.value == "") {
      //   layer.msg("请填写角色名");
      //   this.$refs.roleName.focus();
      // } else if (this.$refs.roleVal.value == "") {
      //   layer.msg("请填选择色值");
      // } else {
      axios
        .put(
          rolesURL + "/" + roleBh,
          {
            roleName: this.personalrole.roleName,
            roleVal: this.personalrole.roleVal,
            isEnable: this.personalrole.isEnable,
            description: this.personalrole.description
          },
          {
            headers: { Authorization: appDB.get("userInfo").token }
          }
        )
        .then(response => {
          let d = response.data;
          if (d.resultCode == 0) {
            this.closeLayer(); //关闭弹窗
            this.$parent.getRoles(); //修改成功以后重新渲染列表获得最新数据
          } else {
            layer.msg(d.msg);
          }
        })
        .catch(error => {
          layer.msg("网络错误");
        });
      // } //else
    }
  }
};
</script>

<style scoped>
.roleAdd {
  width: 80%;
  padding: 50px 10%;
  float: left;
}
.save {
  padding: 8px 40px;
  background: #38b824;
  border-color: #38b824;
  border-radius: 30px;
  float: right;
  margin-left: 30px;
}
.quxiao {
  border: 0;
  background: #cecece;
  padding: 8px 40px;
  border-color: #367fb6;
  border-radius: 30px;
  float: right;
}
</style>


